package cn.ococci.dm.Main.Tools;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * Created by cyan on 15/6/20.
 * 时间工具类
 */
public final class DateTool {

    private static final String FORMAT_SHORT = "yyyy-MM-dd";
    public static final String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
    private static final String[] weekDays = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};

    private static long  clickTime;

    public static boolean delayTime(){

        long now = System.currentTimeMillis();
        if (now - clickTime <= 5000) {
            return false;
        }
        clickTime = now;

        return true;

    }



    public static String currentTime() {
        return dateToString(new Date(), FORMAT_LONG);
    }

    /**
     * 获取String型系统日期 yyyy-MM-dd
     *
     * @return String格式时间
     */
    public static String currentDate() {
        return dateToString(new Date());
    }

    /**
     * 日期转字符串，yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String dateToString(Date date) {
        return dateToString(date, FORMAT_SHORT);
    }

    /**
     * 字符转成字符串 ：yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String dateToStringWithLong(Date date) {
        return dateToString(date, FORMAT_LONG);
    }

    /**
     * 由long类型转换成Date类型
     *
     * @param time
     * @return
     */
    public static Date longToDate(long time) {
        Date dt = new Date(time * 1000);
        return dt;
    }

    public static String long2ToDate(long time) {
        Date dat = new Date(time);
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(dat);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String sb = format.format(gc.getTime());
        return sb;
    }


    /**
     * 日期转字符串，针对任意格式
     *
     * @param date
     * @param format
     * @return
     */
    public static String dateToString(Date date, String format) {
        Locale sysTime = Locale.getDefault();
        SimpleDateFormat sdf = new SimpleDateFormat(format, sysTime);
        return sdf.format(date);
    }

    /**
     * 字符串转日期 yyyy-MM-dd
     *
     * @param dateStr
     * @return
     */
    public static Date stringToDate(String dateStr) {
        Locale sysTime = Locale.getDefault();
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_SHORT, sysTime);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 字符串转成时间
     *
     * @param dateStr yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date stringLongToDate(String dateStr) {
        Locale sysTime = Locale.getDefault();
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_LONG, sysTime);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取指定日期字符串
     *
     * @param date
     * @return
     */
    public static String getWeekString(Date date) {
        return weekDays[date.getDay()];
    }

    /**
     * 小时：分钟
     *
     * @param date
     * @return
     */
    public static String getTimeString(Date date) {

        if (date == null) {
            return "未打卡";
        }

        DateFormat d3 = DateFormat.getTimeInstance();
        return d3.format(date);
    }


    public static String getTimeString(long startTime) {

        SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
        String ms = formatter.format(startTime);

        return ms;

    }


    /**
     * 月份：日
     *
     * @param date
     * @return
     */
    public static String getMonthString(Date date) {

        String[] dateString = dateToString(date).split("-");

        StringBuilder builder = new StringBuilder();
        builder.append(dateString[1]);
        builder.append("月");
        builder.append(dateString[2]);
        builder.append("日");
        builder.append("(");
        builder.append(getWeekString(date));
        builder.append(")");


        return builder.toString();
    }


    //Date转成毫秒
    public static long secondFromDate(Date date) {

        if (date == null) {
            return 0;
        }
        return date.getTime() / 1000;

    }

    //秒数转化为Date
    public static Date dateFromSeconds(long seconds) {

        Date date = new Date();
        try {
            date.setTime(seconds * 1000);
        } catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }

        return date;
    }

    public static String longToM(long time){

        SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
        String ms = formatter.format(time);

        return  ms;
    }



}
